package com.wanhelibs.custom_view.views;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.wanhe.base.BaseDialogFragment;
import com.wanhe.base.OnDialogFragmentDismissListener;
import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.base.SetMealDish;
import com.wanhelibs.R;
import com.wanhelibs.adapter.ChangeSetMealAdapter;

/**
 * Created by XieZaiWei on 2017/8/10.
 * 换菜Dialog
 */

public class ChangeSetMealDialog extends BaseDialogFragment {
	private ListView	lv_setMeal;
	private ChangeSetMealAdapter mChangeSetMealAdapter;

	private SetMealDish mSetMealDish;
	private OnDialogFragmentDismissListener mOnDismissListener;

	public static ChangeSetMealDialog newInstance() {
		ChangeSetMealDialog dialog = new ChangeSetMealDialog();
		return dialog;
	}

	public void setSetMealDish(SetMealDish setMealDish) {
		mSetMealDish = setMealDish;
	}

	public void setOnDismissListener(OnDialogFragmentDismissListener onDismissListener){
		mOnDismissListener = onDismissListener;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		mView = inflater.inflate(R.layout.change_set_meal_dialog, container, false);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		lv_setMeal = (ListView) findViewById(R.id.lv_setMeal);
		lv_setMeal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Dish dish = mSetMealDish.childDish.get(position);
				if(!dish.isChose){
					dish.isChose = true;
					mSetMealDish.currentDish = dish;
					//把所有不是当前选择的都变成false
					for (int i = 0; i < mSetMealDish.childDish.size(); i++) {
						if (i != position) {
							mSetMealDish.childDish.get(i).isChose = false;
						}
					}

					mChangeSetMealAdapter.notifyDataSetChanged();
				}


			}
		});

		findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				dismiss();
			}
		});

		init();
	}

	@Override
	public void onStart() {
		super.onStart();
		Dialog dialog = getDialog();
		if (dialog != null) {
			DisplayMetrics dm = new DisplayMetrics();
			getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
			dialog.getWindow().setLayout((int) (dm.widthPixels * 0.4), (int) (dm.widthPixels * 0.5));
		}
	}

	private void init() {
		if (mSetMealDish.childDish != null && mSetMealDish.childDish.size() > 0) {
			mChangeSetMealAdapter = new ChangeSetMealAdapter(getContext(), mSetMealDish.childDish);
			lv_setMeal.setAdapter(mChangeSetMealAdapter);
		}
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);
		mOnDismissListener.onDialogFragmentDismiss(dialog);
	}

}
